package controller

import (
	"chitchat_http/model"
	"chitchat_http/server"
	"chitchat_http/util"
	"github.com/doujunyu/gogo/job"
)

// ChatByList 聊天列表
func ChatByList(j *job.Job){
	data := server.ChatByList(j.GroupData["user_id"].(int64),j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatBySearchFriend 搜索好友
func ChatBySearchFriend(j *job.Job){
	data := server.ChatBySearchFriend(j.GroupData["user_id"].(int64),j.Input["search"],j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatBySearchGroup 搜索好友
func ChatBySearchGroup(j *job.Job){
	data := server.ChatBySearchGroup(j.GroupData["user_id"].(int64),j.Input["search"],j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByApplyFriendOrApplyGroup 申请加好友，进群，邀请好友进群
func ChatByApplyFriendOrApplyGroup(j *job.Job){
	ReceivedId := util.StringToInt64(j.Input["received_id"])
	groupId := util.StringToInt64(j.Input["group_id"])
	if ReceivedId == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	var err error
	switch util.StringToInt64(j.Input["type_id"]) {
	case model.ChatApplyFriendGroupByTypeIdFriend:
		err = server.ChatByApplyFriend(j.GroupData["user_id"].(int64),ReceivedId,j.Input["content"])
		break
	case model.ChatApplyFriendGroupByTypeIdGroup:
		if groupId == 0{
			j.JsonError(nil,"缺少参数")
			return
		}
		err = server.ChatByApplyGroup(j.GroupData["user_id"].(int64),groupId,j.Input["content"])
		break
	case model.ChatApplyFriendGroupByTypeIdGroupReverse:
		if groupId == 0{
			j.JsonError(nil,"缺少参数")
			return
		}
		err = server.ChatByApplyGroupReverse(j.GroupData["user_id"].(int64),ReceivedId,groupId,j.Input["content"])
	default:
		j.JsonError(nil,"没有对应操作")
		return
	}
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByApplyList 申请列表
func ChatByApplyList(j *job.Job){
	data := server.ChatByApplyList(j.GroupData["user_id"].(int64),j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByApplyCheck 操作申请信息
func ChatByApplyCheck(j *job.Job){
	ApplyId := util.StringToInt64(j.Input["apply_id"])
	status := util.StringToInt64(j.Input["status"])
	if ApplyId == 0 || status == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	err := server.ChatByApplyCheck(j.GroupData["user_id"].(int64),ApplyId,status,j.Input["content"])
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByFriendList 好友列表
func ChatByFriendList(j *job.Job){
	data := server.ChatByFriendList(j.GroupData["user_id"].(int64),j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByFriendListV2 好友列表v2
func ChatByFriendListV2(j *job.Job){
	data := server.ChatByFriendListV2(j.GroupData["user_id"].(int64))
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}


// ChatByFriendRemove 解除好友关系
func ChatByFriendRemove(j *job.Job){
	friendId := util.StringToInt64(j.Input["friend_id"])
	err := server.ChatByFriendRemove(j.GroupData["user_id"].(int64),friendId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatBySendMessage 发送消息
func ChatBySendMessage(j *job.Job){
	typeId := util.StringToInt64(j.Input["type_id"])
	receivedId := util.StringToInt64(j.Input["received_id"])
	err := server.ChatBySendMessage(typeId,j.GroupData["user_id"].(int64),receivedId,j.Input["content"])
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByCreateGroup 创建群组
func ChatByCreateGroup(j *job.Job){
	if j.Input["name"] == "" || j.Input["introduce"] == ""{
		j.JsonError(nil,"缺少参数")
		return
	}
	data,err := server.ChatByCreateGroup(j.GroupData["user_id"].(int64),j.Input["name"],j.Input["introduce"],j.Input["logo"])
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByGroupList 群组列表
func ChatByGroupList(j *job.Job){
	data := server.ChatByGroupList(j.GroupData["user_id"].(int64),j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// ChatByGroupInfo 群组成员
func ChatByGroupInfo(j *job.Job){
	groupId := util.StringToInt64(j.Input["group_id"])
	dataList := server.ChatByGroupFriendList(j.GroupData["user_id"].(int64),groupId,"1","10")
	if len(dataList) == 0 {
		j.JsonSuccess()
		return
	}
	groupData,err :=server.ChatByGroupInfo(j.GroupData["user_id"].(int64),groupId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	groupData["group_friend_list"] = dataList
	j.JsonSuccess(groupData)
	return
}

// ChatByCreateGroupDelete 解散群组
func ChatByCreateGroupDelete(j *job.Job){
	groupId := util.StringToInt64(j.Input["group_id"])
	err := server.ChatByCreateGroupDelete(j.GroupData["user_id"].(int64),groupId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByCreateGroupExit 退出群组
func ChatByCreateGroupExit(j *job.Job){
	groupId := util.StringToInt64(j.Input["group_id"])
	err := server.ChatByCreateGroupExit(j.GroupData["user_id"].(int64),groupId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByCleanMessage 消除接收到得消息
func ChatByCleanMessage(j *job.Job){
	userMessageId := util.StringToInt64(j.Input["user_message_id"])
	err := server.ChatByCleanMessage(j.GroupData["user_id"].(int64),userMessageId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return


}

// ChatByMessageIsTopYes 消息置顶
func ChatByMessageIsTopYes(j *job.Job){
	messageId := util.StringToInt64(j.Input["message_id"])
	if messageId == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	err := server.ChatByMessageIsTopYes(j.GroupData["user_id"].(int64),messageId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// ChatByMessageIsTopNo 消息置顶取消
func ChatByMessageIsTopNo(j *job.Job){
	messageId := util.StringToInt64(j.Input["message_id"])
	if messageId == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	err := server.ChatByMessageIsTopNo(j.GroupData["user_id"].(int64),messageId)
	if err != nil {
		j.JsonError(nil,err.Error())
		return
	}
	j.JsonSuccess()
	return
}

func ChatByMessageList(j *job.Job){
	typeId := util.StringToInt64(j.Input["type_id"])
	if typeId == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	receivedId := util.StringToInt64(j.Input["received_id"])
	if receivedId == 0{
		j.JsonError(nil,"缺少参数")
		return
	}
	data := server.ChatByMessageList(typeId,j.GroupData["user_id"].(int64),receivedId,j.Input["page"],j.Input["page_size"])
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return

}






